package com.bosssoft.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.entity.dto.FibRoleAuthDTO;
import com.bosssoft.entity.po.FibBankPO;
import com.bosssoft.entity.po.FibRoleAuthPO;
import com.bosssoft.entity.vo.FibBankVO;
import com.bosssoft.entity.vo.FibRoleAuthVO;
import com.bosssoft.entity.vo.FibUserVO;
import com.bosssoft.enums.CopyPropertityEnum;
import com.bosssoft.exception.DaoException;
import com.bosssoft.page.PageResult;
import com.bosssoft.response.Result;
import com.bosssoft.service.FibRoleAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 项昊天
 * @create 2021/8/17 20:49
 */
@Slf4j
@RequestMapping("/roleauth")
@RestController
public class RoleAuthController {

    @Resource
    FibRoleAuthService roleAuthService;

    @PostMapping("/save")
    public Result saveRoleAuth(@RequestBody @Validated FibRoleAuthVO roleAuthVO) {
        try {
            Byte deleted = 1;
            roleAuthVO.setDeleted(deleted);
            FibRoleAuthDTO roleAuthDTO = new FibRoleAuthDTO();
            BeanUtils.copyProperties(roleAuthVO, roleAuthDTO);
            if (roleAuthService.addRoleAuth(roleAuthDTO)) {
                return Result.ok().message("添加角色权限联系成功!");
            }
            return Result.error().message("添加角色权限联系失败!");
        } catch (Exception e) {
            throw DaoException.error().setCodeAndMessage(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }

    @PutMapping("/modify")
    public Result modifyRoleAuth(@RequestBody @Validated FibRoleAuthVO roleAuthVO) {
        try {
            FibRoleAuthDTO roleAuthDTO = new FibRoleAuthDTO();
            BeanUtils.copyProperties(roleAuthVO, roleAuthDTO);
            if (roleAuthService.modifyRoleAuth(roleAuthDTO)) {
                return Result.ok().message("修改角色权限联系成功!");
            }
            return Result.error().message("修改角色权限联系失败!");
        } catch (Exception e) {
            throw DaoException.error().setCodeAndMessage(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }

    @PostMapping("/getRoleAuthPage")
    public Result getRoleAuthPage(@RequestParam(defaultValue = "1") Integer pageNo,
                                  @RequestParam(defaultValue = "10") Integer pageSize,
                                  FibRoleAuthVO fibRoleAuthVO) {
        try {
            Page<FibRoleAuthPO> page = new Page<>(pageNo, pageSize);
            QueryWrapper<FibRoleAuthPO> queryWrapper = new QueryWrapper<>();
            if (ObjectUtil.isNotNull(fibRoleAuthVO.getAuthId())) {
                queryWrapper.like("auth_id", fibRoleAuthVO.getAuthId());
            }
            if (ObjectUtil.isNotNull(fibRoleAuthVO.getRoleId())) {
                queryWrapper.like("role_id", fibRoleAuthVO.getRoleId());
            }
            Page<FibRoleAuthPO> roleAuthPage = roleAuthService.page(page, queryWrapper);
            long total = roleAuthPage.getTotal();
            List<FibRoleAuthPO> recordPOs = roleAuthPage.getRecords();
            PageResult<FibRoleAuthPO> results = new PageResult<>(page, recordPOs);
            return Result.ok().data("total", total)
                    .data("page", results)
                    .data("pageNo", pageNo);
        } catch (Exception e) {
            log.info("getRoleAuthPage出现异常");
            throw DaoException.error(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }

    @PutMapping("/ban/{id}/{status}")
    public Result banRoleAuth(@PathVariable("id") Long id,
                              @PathVariable("status") Integer status) {
        if (roleAuthService.banRoleAuthById(id, status)) {
            return Result.ok().message("操作成功");
        }
        return Result.error().message("操作失败");
    }

    @DeleteMapping("/delete")
    public Result delRoleAuth(@RequestBody FibRoleAuthVO vo) {
        if (vo.getId() != null && roleAuthService.deleteRoleAuthById(vo.getId())) {
            return Result.ok().message("删除角色权限联系成功!");
        }
        return Result.error().message("删除角色权限联系失败!");
    }

//  @DeleteMapping("/delete/{id}")
//  public Result delRoleAuth(@PathVariable("id") Long id) {
//    if (roleAuthService.deleteRoleAuthById(id)) {
//      return Result.ok().message("删除角色权限联系成功!");
//    }
//    return Result.error().message("删除角色权限联系失败!");
//  }
}
